home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / tpsorts / sortdemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-10-04  |  12.4 KB  |  509 lines

  1. Program SortDemo;
  2.  
  3. {**************************************************************************
  4. *                                                                         *
  5. * Description:                                                            *
  6. *                                                                         *
  7. *   This program graphically displays the functioning of 8 of the most    *
  8. *   common sorting algorithms in use today.                               *
  9. *                                                                         *
  10. *                                                                         *
  11. * Author:  Richard R. Rebouche                                            *
  12. *                                                                         *
  13. * Update:  05/15/85                                                       *
  14. *                                                                         *
  15. *                                                                         *
  16. * Comments:                                                               *
  17. *                                                                         *
  18. *   This program was written to kill an afternoon in celebration of       *
  19. *   the end of finals.                                                    *
  20. *                                                                         *
  21. *   I would encourage anyone interested in the topic of sorting to        *
  22. *   refer to Chap. 7 of Wirth's book `Algorithms + Data Structures'.      *
  23. *   This book served as the reference for the more complex algorithms     *
  24. *   presented here, such as the Quick Sort.                               *
  25. *                                                                         *
  26. *   I would like to know if anyone makes enhancements or extensions to    *
  27. *   this program, so please upload any improvements.                      *
  28. *                                                                         *
  29. *                                                                         *
  30. * BBS:                                                                    *
  31. *                                                                         *
  32. *   This source code will originate from John Friel's BBS in Cedar Falls, *
  33. *   IA.  PH: (319) 266-8086                                               *
  34. *                                                                         *
  35. **************************************************************************}
  36.  
  37.  
  38. Const NumItems = 200;
  39.  
  40. Type Sort_Array_Type = Array [0..NumItems] of Integer;
  41.  
  42.    { Note: Must start at zero because of insertion sort }
  43.    {       All other sorts consider the arrays to begin }
  44.    {       at base one.                                 }
  45.  
  46.  
  47. Var I, J     : Integer;
  48.     OrgArray : Sort_Array_Type;
  49.     NumArray : Sort_Array_Type;
  50.     Done     : Boolean;
  51.     C        : Char;
  52.  
  53.  
  54.  
  55.  
  56.  
  57. { Interchange two integers, erasing and redrawing them on the screen }
  58.  
  59. Procedure Exchange (Y1, Y2 : Integer;  Var X1, X2 : Integer);
  60.  
  61. Var X3 : Integer;
  62.  
  63. Begin
  64.   Plot (X1, Y1-1, 0);     { Erase old points   }
  65.   Plot (X2, Y2-1, 0);
  66.   X3 := X1;               { Interchange values }
  67.   X1 := X2;
  68.   X2 := X3;
  69.   Plot (X1, Y1-1, 1);     { Draw new points    }
  70.   Plot (X2, Y2-1, 1);
  71. End;
  72.  
  73.  
  74.  
  75.  
  76. { Assign a new value to an integer, erasing and redrawing it }
  77.  
  78. Procedure AssignValue (Y : Integer;  Var X: Integer;  N : Integer);
  79.  
  80. Begin
  81.   Plot (X, Y-1, 0);     { Erase old point  }
  82.   X := N;               { Assign new value }
  83.   Plot (X, Y-1, 1);     { Draw new point   }
  84. End;
  85.  
  86.  
  87.  
  88.  
  89. { Prompt the user to press the SPACEBAR }
  90.  
  91. Procedure Done_Prompt;
  92.  
  93. Var C : Char;
  94.  
  95. Begin
  96.   GotoXY (1, 24);
  97.   Write ('Press the SPACEBAR to continue . . .');
  98.   C := Chr(0);
  99.   While C <> ' ' Do
  100.     Read (Kbd, C);
  101. End;
  102.  
  103.  
  104.  
  105.  
  106. { Fill an array with random numbers between 0 and 639, inclusive }
  107.  
  108. Procedure Fill_Array (Var A : Sort_Array_Type);
  109.  
  110. Var I : Integer;
  111.  
  112. Begin
  113.   For I := 1 to NumItems do
  114.     A[I] := Random(640);
  115. End;
  116.  
  117.  
  118.  
  119.  
  120. { Print the contents of an array }
  121.  
  122. Procedure PrintArray (Var A : Sort_Array_Type);
  123.  
  124. Var I, N : Integer;
  125.  
  126. Begin
  127.   Writeln;
  128.   For I := 1 to NumItems do
  129.     Begin
  130.       Write (A[i]:4);
  131.       If WhereX > 75 Then
  132.         Writeln;
  133.     End;
  134.   Writeln;
  135. End;
  136.  
  137.  
  138.  
  139.  
  140. { Plot the contents of an array onto the graphics screen }
  141.  
  142. Procedure PlotArray (Var A : Sort_Array_Type);
  143.  
  144. Var I, J, N : Integer;
  145.     C       : Char;
  146.  
  147. Begin
  148.   Hires;
  149.   Hirescolor (white);
  150.   For I := 1 to NumItems do
  151.     Plot (A[I], I-1, 1);
  152. End;
  153.  
  154.  
  155.  
  156.  
  157. { Sort an array using the `Bubble' algorithm }
  158.  
  159. Procedure BubbleSort (Var A : Sort_Array_Type);
  160.  
  161. Var I, J, N : Integer;
  162.  
  163. Begin
  164.   For I := 2 to NumItems do
  165.     For J := NumItems DownTo I do
  166.       If A[J-1] > A[J] Then
  167.         Exchange (J-1, J, A[J-1], A[J]);
  168. End;
  169.  
  170.  
  171.  
  172.  
  173. { Sort an array using the `Shaker' (bi-directional bubble) algorithm }
  174.  
  175. Procedure ShakerSort (Var A : Sort_Array_Type);
  176.  
  177. Var J, K, L, R : Integer;
  178.     X          : Integer;
  179.  
  180. Begin
  181.   L := 1; R := NumItems; K := NumItems;
  182.   Repeat
  183.     For J := R DownTo L do
  184.       If A[J-1] > A[J] then
  185.         Begin
  186.           Exchange (J-1, J, A[J-1], A[J]);
  187.           K := J;
  188.         End;
  189.     L := K + 1;
  190.     For J := L To R do
  191.       If A[J-1] > A[J] then
  192.         Begin
  193.           Exchange (J-1, J, A[J-1], A[J]);
  194.           K := J;
  195.         End;
  196.     R := K-1;
  197.   Until L > R;
  198. End;
  199.  
  200.  
  201.  
  202.  
  203.  
  204. { Sort an array using the `Insertion' algorithm }
  205.  
  206. Procedure InsertionSort (Var A : Sort_Array_Type);
  207.  
  208. Var I,J, X : Integer;
  209.  
  210. Begin
  211.   For I := 2 to NumItems do
  212.     Begin
  213.       X := A[I];  A[0] := X;  J := I-1;
  214.       While X < A[J] do
  215.         Begin
  216.           AssignValue (J+1, A[J+1], A[J]);  J := J - 1;
  217.         End;
  218.       AssignValue(J+1, A[J+1], X);
  219.     End;
  220. End;
  221.  
  222.  
  223.  
  224.  
  225. { Sort an array using the `Binary Insertion' algorithm }
  226.  
  227. Procedure BinaryInsertionSort (Var A : Sort_Array_Type);
  228.  
  229. Var I,J,L,R,M,X : Integer;
  230.  
  231. Begin
  232.   For I := 2 to NumItems do
  233.     Begin
  234.       X := A[I];  L := 1;  R := I-1;
  235.       While L <= R do
  236.         Begin
  237.           M := (L+R) Div 2;
  238.           If X < A[M] Then
  239.             R := M - 1
  240.           Else L := M+1
  241.         End;
  242.       For J := I-1 DownTo L do
  243.         AssignValue (J+1, A[J+1], A[J]);
  244.       AssignValue (L, A[L], X);
  245.     End;
  246. End;
  247.  
  248.  
  249.  
  250.  
  251. { Sort an array using the `Selection' algorithm }
  252.  
  253. Procedure SelectionSort (Var A : Sort_Array_Type);
  254.  
  255. Var I, J, K, X : Integer;
  256.  
  257. Begin
  258.   For I := 1 to NumItems - 1 do
  259.     Begin
  260.       K := I;  X := A[I];
  261.       For J := I+1 To NumItems do
  262.         If A[J] < X then
  263.           Begin
  264.             K := J;  X := A[J];
  265.           End;
  266.       AssignValue (K, A[K], A[I]);
  267.       AssignValue (I, A[I], X);
  268.     End;
  269. End;
  270.  
  271.  
  272.  
  273.  
  274.  
  275. { Sort an array using the `Shell' algorithm (6 parts, binary progression) }
  276.  
  277. Procedure ShellSort (Var A : Sort_Array_Type);
  278.  
  279.   Const T = 6;
  280.         H : Array [1..T] Of Integer = (33,17,9,5,3,1);
  281.  
  282.   Var I,J,K,S, M, X : Integer;
  283.  
  284. Begin
  285.   For M := 1 To T Do
  286.     Begin
  287.       K := H[M];  S := -K; {sentinal position}
  288.       For I := K+1 To NumItems do
  289.         Begin
  290.           X := A[I]; J := I-K;
  291.           If S = 0 Then
  292.             S := -K; S:= S+1; AssignValue(S, A[S], X);
  293.           While X < A[J] do
  294.             Begin
  295.               AssignValue (J+K, A[J+K], A[J]);
  296.               J := J - K;
  297.             End;
  298.           AssignValue (J+K, A[J+K], X);
  299.         End;
  300.     End;
  301. End;
  302.  
  303.  
  304.  
  305.  
  306.  
  307. { Sort an array using the `Heap' algorithm }
  308.  
  309. Procedure HeapSort (Var A : Sort_Array_Type);
  310. Var L,R,X : Integer;
  311.  
  312.   Procedure Sift;
  313.     Label 13;
  314.     Var I,J : integer;
  315.   Begin
  316.     I := L;  J := 2 *I;  X := A[I];
  317.     While J <= R do
  318.       Begin
  319.         If J < R Then
  320.           If A[J] < A[J+1] Then
  321.             J := J + 1;
  322.         If X >= A[J] Then
  323.           GoTo 13;
  324.         AssignValue(I, A[I], A[J]);  I := J;  J := 2 * I;
  325.       End;
  326.     13:AssignValue(I, A[I], X);
  327.   End;
  328.  
  329. Begin
  330.   L := (NumItems Div 2) + 1;  R:= NumItems;
  331.   While L > 1 do
  332.     Begin
  333.       L := L-1; Sift;
  334.     End;
  335.   While r > 1 do
  336.     Begin
  337.       Exchange (1, R, A[1], A[R]);
  338.       R := R - 1;
  339.       Sift;
  340.     End;
  341. End;
  342.  
  343.  
  344.  
  345.  
  346. { Sort an array using the `Quick' algorithm (recursive form) }
  347.  
  348. procedure quicksort (Var A : Sort_Array_Type);
  349.  
  350.   Procedure Sort (L, R : Integer);
  351.     Var I, J, X : Integer;
  352.   Begin
  353.     I := L;  J := R;
  354.     X := A[(L+R) Div 2];
  355.     Repeat
  356.       While A[I] < X do
  357.         I := I + 1;
  358.       While X < A[J] do
  359.         J := J - 1;
  360.       If I <= J then
  361.       Begin
  362.         Exchange (I, J, A[I], A[J]);
  363.         I := I + 1;  J := J - 1;
  364.       End;
  365.     Until I > J;
  366.     If L < J Then
  367.       Sort (L, J);
  368.     If I < R Then
  369.       Sort (I, R);
  370.   End;
  371.  
  372. Begin
  373.   Sort (1, NumItems);
  374. End;
  375.  
  376.  
  377.  
  378.  
  379.  
  380. { Display the opening screen }
  381.  
  382. procedure Do_Title_Screen;
  383.  
  384. Var I, J : Integer;
  385.  
  386. Begin
  387.   ClrScr;
  388.   GotoXY (28, 1);  Write ('************************');
  389.   GotoXY (28, 2);  Write ('*                      *');
  390.   GotoXY (28, 3);  Write ('*  Sort Demonstration  *');
  391.   GotoXY (28, 4);  Write ('*                      *');
  392.   GotoXY (28, 5);  Write ('*   Update: 05/15/85   *');
  393.   GotoXY (28, 6);  Write ('*                      *');
  394.   GotoXY (28, 7);  Write ('************************');
  395.   Window (9, 1, 80, 25);
  396.   GotoXY (1, 10);
  397.   WriteLn ('This program illustrates eight of the most common array-sorting');
  398.   WriteLn ('algorithms in use today.');
  399.   WriteLn;
  400.   WriteLn ('The sorts are applied to a 200 element array containing integer');
  401.   writeLn ('values ranging from 0 to 639, inclusive.');
  402.   WriteLn;
  403.   WriteLn ('Subscripts start at the top of the screen and work down.');
  404.   WriteLn;
  405.   WriteLn ('Numeric values start at the left of the screen and work right.');
  406.   WriteLn;
  407.   WriteLn;
  408.   WriteLn ('               Enjoy!   -   Richard R. Rebouche');
  409.   Window (1, 1, 80, 25);
  410.   Done_Prompt;
  411. End;
  412.  
  413.  
  414.  
  415. { Display the program menu, return the selection }
  416.  
  417. Function Get_Choice : Char;
  418.  
  419. Var I, J : Integer;
  420.     C    : Char;
  421.  
  422. Begin
  423.   Textmode;
  424.   ClrScr;
  425.   GotoXY (25,3);   WriteLn ('******************************');
  426.   GotoXY (25,4);   WriteLn ('*                            *');
  427.   GotoXY (25,5);   WriteLn ('* Sort Demonstration Program *');
  428.   GotoXY (25,6);   WriteLn ('*                            *');
  429.   GotoXY (25,7);   WriteLn ('******************************');
  430.   WriteLn;
  431.   WriteLn;
  432.   WriteLn ('1 - ':29,  'Bubble Sort');
  433.   WriteLn ('2 - ':29,  'Shaker Sort');
  434.   WriteLn ('3 - ':29,  'Straight Insertion Sort');
  435.   WriteLn ('4 - ':29,  'Binary Insertion Sort');
  436.   WriteLn ('5 - ':29,  'Selection Sort');
  437.   WriteLn ('6 - ':29,  'Shell Sort');
  438.   WriteLn ('7 - ':29,  'Heap Sort');
  439.   WriteLn ('8 - ':29,  'Quick Sort');
  440.   WriteLn ('V - ':29,  'View Current Data Set');
  441.   WriteLn ('G - ':29,  'Generate New Data Set');
  442.   WriteLn ('Q - ':29,  'Terminate Demonstration');
  443.   WriteLn;
  444.   WriteLn;
  445.   C := ' ';
  446.   Write ('Selection: ':43);
  447.   While Not (C In ['1'..'8', 'V', 'G', 'Q']) do
  448.     Begin
  449.       Read (Kbd, C);
  450.       C := UpCase (C);
  451.     End;
  452.   Writeln (C);
  453.   Get_Choice := C;
  454. End;
  455.  
  456.  
  457.  
  458.  
  459. { Set up and call the sort procedures based upon 'N' }
  460.  
  461. Procedure PerformSort (N : Integer);
  462. Begin
  463.   NumArray := OrgArray;
  464.   PlotArray (NumArray);
  465.   Case N of
  466.     1 : BubbleSort (NumArray);
  467.     2 : ShakerSort (NumArray);
  468.     3 : InsertionSort (NumArray);
  469.     4 : BinaryInsertionSort (NumArray);
  470.     5 : SelectionSort (NumArray);
  471.     6 : ShellSort (NumArray);
  472.     7 : HeapSort (NumArray);
  473.     8 : QuickSort (NumArray);
  474.   End;
  475.   Done_Prompt;
  476. End;
  477.  
  478.  
  479.  
  480. Begin
  481.   Fill_Array (OrgArray);
  482.   Done := false;
  483.   Do_Title_Screen;
  484.   While Not Done do
  485.     Begin
  486.       C := Get_Choice;
  487.       ClrScr;
  488.       If C In ['1'..'8'] then
  489.         PerformSort (Ord(C) - Ord('0'))
  490.       Else
  491.         Case C of
  492.           'V' : Begin
  493.                   WriteLn ('Current Data Collection:');
  494.                   GotoXY (1, WhereY + 5);
  495.                   PrintArray (OrgArray);
  496.                   Done_Prompt;
  497.                 End;
  498.           'G' : Begin
  499.                   WriteLn ('Generating New Data Collection:');
  500.                   Fill_Array (OrgArray);
  501.                   GotoXY (1, WhereY + 5);
  502.                   PrintArray (OrgArray);
  503.                   Done_Prompt;
  504.                 End;
  505.           'Q' : Done := True;
  506.        End; {Case}
  507.     End; {While}
  508. End.